package util;

import java.util.ArrayList;

public class WineCategory {
	private Category category;
	private String[] factors;
	private ArrayList<WineVarietal> varietals = new ArrayList<WineVarietal>();
	
	public WineCategory(Category category, String[] factors){
		this.category = category;
		this.factors = factors;
	}
	
	public int rank(String ingredientList){
		int rank = 0;
		for(String factor : factors){
			if(ingredientList.contains(factor)){
				rank++;
			}
		}
		return rank;
	}
	
	public Category getCategory(){
		return this.category;
	}
	
	public ArrayList<VarietalRanking> getVarietalRankings(String ingredientList){
		ArrayList<VarietalRanking> rankingList = new ArrayList<VarietalRanking>();
		for(WineVarietal varietal : varietals){
			addRanking(rankingList, varietal.rank(ingredientList));
		}
		return rankingList;
	}
	
	private void addRanking(ArrayList<VarietalRanking> rankingList, VarietalRanking ranking){
		if(rankingList.size() == 0){
			rankingList.add(ranking);
			return;
		}
		for(int i = 0; i < rankingList.size(); i ++){
			VarietalRanking r = rankingList.get(i);
			if(ranking.compareTo(r) >= 0){
				rankingList.add(i, ranking);
				return;
			}
			else if(i == rankingList.size() - 1){
				rankingList.add(ranking);
				return;
			}
		}
	}
	
	public void addVarietal(WineVarietal varietal){
		varietals.add(varietal);
	}
}
